package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.StudyResourceType;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.StudyResourceTypeMapper;
import com.cyz.user.userService.StudyResourceTypeIdService;
import com.cyz.user.userService.StudyResourceTypeService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.user.vo.StudyResourceTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 34928
* @description 针对表【study_resource_type】的数据库操作Service实现
* @createDate 2023-02-22 14:32:32
*/
@Service
public class StudyResourceTypeServiceImpl extends ServiceImpl<StudyResourceTypeMapper, StudyResourceType>
    implements StudyResourceTypeService {

    @Autowired
    private StudyResourceTypeIdService studyResourceTypeIdService;

    @Override
    public R queryStudyResourceTypes() {
        //查询数据
        List<StudyResourceType> list = list();
        //
        List<StudyResourceTypeVo> studyResourceTypeVos = BeanCopyUtil.copyBeanList(list, StudyResourceTypeVo.class);
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(studyResourceTypeVos);
    }

    @Override
    public R queryStudyResourceTypesAndStudyResources() {
        //查询数据
        List<StudyResourceType> list = list();
        //vo处理
        List<StudyResourceTypeVo> studyResourceTypeVos = BeanCopyUtil.copyBeanList(list, StudyResourceTypeVo.class);
        //查询各分组里面的资源
        studyResourceTypeVos
                .forEach(item->item.setStudyResourceVos(studyResourceTypeIdService.getStudyResourcesByTypeId(item.getId())));
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(studyResourceTypeVos);
    }
}




